var WXBizDataCrypt = require('utils/RdWXBizDataCrypt.js');
var AppId = 'wxwxaa93ef54763f94c9'
var AppSecret = '4ccb16180f4157292fc2790f11cfd433'

//app.js
App({
  onLaunch: function () {
    if (wx.getStorageSync("LoginNo") && wx.getStorageSync("LoginNo") != null && wx.getStorageSync("LoginNo") != '') {
      this.globalData.LoginNo = wx.getStorageSync("LoginNo")
    }
    if (wx.getStorageSync("Password") && wx.getStorageSync("Password") != null && wx.getStorageSync("Password") != '') {
      this.globalData.Password = wx.getStorageSync("Password")
    }
    
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)

    // 登录
    // wx.login({
    //   success: res => {
    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
    //   }
    // })

    // {
    //   "pagePath": "pages/index/index",
    //     "text": "主页",
    //       "iconPath": "./pages/images/home.png",
    //         "selectedIconPath": "./pages/images/home-active.png"
    // },

    
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    LoginNo: '',
    Password: '',
    userInfo: null,
    api:"http://47.111.184.145:92/api/",
    Authorization: null,
    appId: "wxaa93ef54763f94c9",
    sceret: "4ccb16180f4157292fc2790f11cfd433",
    offerList:[],
    freight: 0
  },
  onShow: function () {
    var that=this
    this.checkLogin()
  },
  checkLogin: function(){
    var that = this
    that.globalData.LoginNo = wx.getStorageSync('LoginNo')
    that.globalData.Password = wx.getStorageSync('Password')
    console.log(that.globalData.LoginNo, that.globalData.Password)
    if (that.globalData.LoginNo != '' && that.globalData.Password != '') {
      wx.request({
        url: that.globalData.api + 'Customer/Login',
        method: "POST",
        data: {
          "loginNo": that.globalData.LoginNo,
          "password": that.globalData.Password,
          "loginSource": "customer"
        },
        success: function (res) {
          if (res.data.jwtBearer) {
            that.globalData.Authorization = res.data.jwtBearer
            // wx.setStorage({
            //   key: 'Authorization',
            //   data: res.data.Result,
            // })
            // wx.switchTab({
            //   url: '/pages/operate/operate'
            // })
          }
        }
      })
    }
  },
  req: function(obj){
    var that = this
    var method = obj.method || "GET";
    var url = that.globalData.api + obj.url;
    var data = obj.data || {};
    var dataType = obj.dataType || "json";
    var success = obj.success;
    var token = null
    var result = null
    if (that.globalData.Authorization) {
      token = 'bearer '+that.globalData.Authorization
    }
    wx.request({
      url: url,
      method: method,
      header: {
        Authorization: token
      },
      dataType: dataType,
      data: data,
      success: function(res){
        if (res.statusCode == 401) {
          that.globalData.Authorization = null,
          // wx.removeStorage({
          //   key: 'LoginNo',
          //   success(res) {
          //     console.log(res)
          //   }
          // })
          // wx.removeStorage({
          //   key: 'Password',
          //   success(res) {
          //     console.log(res)
          //   }
          // })
          wx.showToast({
            title: '网络错误，请重试',
            icon: 'loading',
            duration: 2000,
            success: function () {
              // wx.redirectTo({
              //   url: "/pages/user/login/login"
              // })
              that.checkLogin()
            }
          })
          return false
        }else{
          success(res)
        }
      },
      fail: function(a,b,c){
        console.log("fail")
        console.log(a,b,c)
      }
    })
  },
  dateFormat: function(d){
    var arr=d.split("T")
    return arr
  }
})